#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <nanomsg/pair.h>
#include <nanomsg/bus.h>
#include <nanomsg/nn.h>

#include <iostream>
#include <chrono>
#include <map>
#include <unordered_map>
#include <ext/hash_map>
#include <backward/hash_fun.h>
#include <ctime>
#include <string>
using namespace std;

#define BUF_LEN  100
 
char *url = (char*)"tcp://127.0.0.1:2021";
 
 int64_t get_current_timestamp_us()
{
	std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds> tp =
		std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
	return tp.time_since_epoch().count();
}

int64_t get_current_timestamp_ns()
{
	std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> tp =
		std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now());
	return tp.time_since_epoch().count();
}
void testmap()
{
    srand(time(nullptr));
    int nmaxtimes = 1000;
    std::map<int, int> basket_order_map_;  
    for (int i = 0; i < nmaxtimes;i++)
    {
        basket_order_map_[i] = rand();
    }
     for (int j = 0; j < 50;j++)
     {

     
    int nkey = rand()%nmaxtimes;
    int64_t start = get_current_timestamp_us();
    auto itfind = basket_order_map_.find(nkey);
    if (itfind != basket_order_map_.end())
    {
        std::cout << "key = " << nkey << ",find value :" << itfind->second << ",time :" << get_current_timestamp_us()-start << std::endl;
    }
    else
    {
        std::cout << "find not time " << get_current_timestamp_us()-start << std::endl;
    }
    basket_order_map_[j+nmaxtimes] = rand();
     }
}
void testunmap()
{
    srand(time(nullptr));
    int nmaxtimes = 1000;
    std::unordered_map<int, int> basket_order_map_;  
    for (int i = 0; i < nmaxtimes;i++)
    {
        basket_order_map_[i] = rand();
    }
     for (int j = 0; j < 50;j++)
     {    
    int nkey = rand()%nmaxtimes;
    int64_t start = get_current_timestamp_us();
    auto itfind = basket_order_map_.find(nkey);
    if (itfind != basket_order_map_.end())
    {
        std::cout << "key = " << nkey << ",find value :" << itfind->second << ",time :" << get_current_timestamp_us()-start << std::endl;
    }
    else
    {
        std::cout << "find not time " << get_current_timestamp_us()-start << std::endl;
    }
    basket_order_map_[j+nmaxtimes] = rand();
     }
}

void testhash_map()
{
    srand(time(nullptr));
    int nmaxtimes = 1000;
    __gnu_cxx::hash_map<int, string> basket_order_map_;  
    for (int i = 0; i < nmaxtimes;i++)
    {
        basket_order_map_[i] = to_string(rand());
    }
    for (int j = 0; j < 50;j++)
   {

   int nkey = rand()%nmaxtimes;
    int64_t start = get_current_timestamp_us();
    auto itfind = basket_order_map_.find(nkey);
    if (itfind != basket_order_map_.end())
    {
        std::cout <<"haskmap key = " << nkey << ",find value :" << itfind->second << ",time :" << get_current_timestamp_us()-start << std::endl;
    }
    else
    {
        std::cout << "haskmap find not time " << get_current_timestamp_us()-start << std::endl;
    }
}
}
int main(void)
{
   //testmap();
   testunmap();
   //testhash_map();
    return 0;

 int client_sock = 0;
 char buf[BUF_LEN] = {0};
 
 if (client_sock = nn_socket (AF_SP, NN_PAIR) < 0)
 {
  printf("create server socket failed!\n");
  return -1;
 }
 
 if (nn_connect(client_sock, url) < 0) 
 {
  printf("connect server sock failed!\r\n");
  nn_close(client_sock);
  return -1;
 }
 
 printf("client init success!\n");
 
 while (1)
 {
  //scanf("%s", buf);
  sprintf(buf,"%ld",get_current_timestamp_us());
  if (nn_send(client_sock, buf, sizeof(buf), 0) < 0)
  {
   printf("send failed!\r\n");
   nn_close(client_sock);
  }
 
  /*memset(buf, 0, BUF_LEN);   
 
  if (nn_recv(client_sock, buf, sizeof(buf), 0) > 0) 
  {
   printf("recieve server msg: %s\r\n", buf);
  }
  memset(buf, 0, BUF_LEN);   */
 }
 
 nn_close(client_sock);
 
 return 0;
}
